<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Polygon extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('Polygon_model','m');
    $this->load->helper('url');
	$this->load->library('session');
  }

  
  public function index()
  {

	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

  
    //load library table agar mudah membuat tabel
    $this->load->library('table');
	$this->table->set_heading('ID', 'strokeColor', 'strokeOpacity','strokeWeight','fillColor','fillOpacity');

    $all_polygon = $this->m->all();
    foreach($all_polygon as $polygon){
      $this->table->add_row(
        $polygon->id,
        //$polygon->shape,
        $polygon->strokeColor,
        $polygon->strokeOpacity,
        $polygon->strokeWeight,
        $polygon->fillColor,
        $polygon->fillOpacity,
		
        anchor('polygon/view/'.$polygon->id,'Lihat'),
        anchor('polygon/peta/'.$polygon->id,'Lihat Peta'),
        anchor('polygon/edit/'.$polygon->id,'Edit')
      );
    }
    echo $this->table->generate();
    echo anchor('polygon/add','Add');
  }



  public function add()
  {

	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

  
    //untuk memudahkan membuat form
    $this->load->helper('form');
    //load library table agar mudah membuat tabel
    $this->load->library('table');

    //buka form
    echo form_open('polygon/save');
    $this->table->add_row('Shape',':',form_textarea('shape'));	
    $this->table->add_row('strokeColor',':',form_input('strokeColor','#FF0000'));	
    $this->table->add_row('strokeOpacity',':',form_input('strokeOpacity','0.8'));	
    $this->table->add_row('strokeWeight',':',form_input('strokeWeight','2'));	
    $this->table->add_row('fillColor',':',form_input('fillColor','#0000FF'));	
    $this->table->add_row('fillOpacity',':',form_input('fillOpacity',0.6));	

    //tombol tambah
    echo form_submit('tambah', 'Tambah');

    echo $this->table->generate();
	
    //tutup form
    echo form_close();
  }

  public function save()
  {
    //save shape
    $data['shape']=$this->input->post('shape');
    $data['strokeColor']=$this->input->post('strokeColor');
    $data['strokeOpacity']=$this->input->post('strokeOpacity');
    $data['strokeWeight']=$this->input->post('strokeWeight');
    $data['fillColor']=$this->input->post('fillColor');
    $data['fillOpacity']=$this->input->post('fillOpacity');

    //simpan id autoincrement yang dihasilkan untuk ditampilkan jika sukses disimpan
    $id = $this->m->insert($data);
		redirect('polygon/view/'.$id);

  }

  public function view($id=1)
  {

	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');
  
    $this->load->library('table');
    $o = $this->m->get($id);
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);
    $this->table->add_row('Shape',':',$o->shape);
    $this->table->add_row(anchor('polygon/add','Add'),anchor('polygon/edit/'.$id,'Edit'));
    echo $this->table->generate();
  }

  public function edit($id)
  {

	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

  
    $this->load->library('table');
    $this->load->helper('form');
    $o = $this->m->get($id);

    echo form_open('polygon/update/'.$o->id);
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);
    $this->table->add_row('shape',':',form_textarea('shape', $o->shape));

    $this->table->add_row('strokeColor',':',form_input('strokeColor','#FF0000'));	
    $this->table->add_row('strokeOpacity',':',form_input('strokeOpacity','0.8'));	
    $this->table->add_row('strokeWeight',':',form_input('strokeWeight','2'));	
    $this->table->add_row('fillColor',':',form_input('fillColor','#0000FF'));	
    $this->table->add_row('fillOpacity',':',form_input('fillOpacity',0.6));		
	
    $this->table->add_row(form_submit('update', 'Update'));

    echo $this->table->generate();
    echo form_close();


  }

  public function update($id){
    //update
    $data['shape']=$this->input->post('shape');
    $data['strokeColor']=$this->input->post('strokeColor');
    $data['strokeOpacity']=$this->input->post('strokeOpacity');
    $data['strokeWeight']=$this->input->post('strokeWeight');
    $data['fillColor']=$this->input->post('fillColor');
    $data['fillOpacity']=$this->input->post('fillOpacity');	
    $this->m->update($id,$data);
		redirect('polygon/view/'.$id);

  }



	public function shape($id){
		header('Content-Type:application/x-javascript; charset=utf-8');

		//get our object
		$o=$this->m->get($id);
		echo 'var polygon'.$id."_paths = [\n";
		//try to use preg_split instead of explode ... kadang-kadang error di \r\n
		$ar_shape = preg_split('~\R~',$o->shape);
		//print_r($ar_shape);
		//foreach($ar_shape as $a)
		//  echo 'new google.maps.LatLng('.$a."),\n";
		$p=$this->m->getPolygonOptions($id);
		echo "\tnew google.maps.LatLng(";
		echo implode("),\n\tnew google.maps.LatLng(",$ar_shape);
		echo ")\n];";

		echo "\nvar polygon".$id."_polyOptions = {";
		echo "\n\tpath: polygon".$id."_paths,";
		echo "\n\t strokeColor: \"".$p->strokeColor."\",
		strokeOpacity: ".$p->strokeOpacity.",
		strokeWeight: ".$p->strokeWeight.",
		fillColor: \"".$p->fillColor."\",
		fillOpacity: ".$p->fillOpacity."
	}";

		echo"\n\npolygon".$id."_polygon = new google.maps.Polygon(polygon".$id."_polyOptions);";

	}

	public function peta($id){
		$o=$this->m->get($id);

		$data['title'] ='polygon '.$o->id;
		$data['js']= site_url('polygon/shape/'.$id);
		$data['js_initialize']='polygon'.$id.'_polygon.setMap(map);';
		$this->load->view('polygon',$data);  
	}

}
